package pipe

import (
	"fmt"
	"fur/src/dbs"
	"log"
	"strconv"
	"sync/atomic"
	"time"
)

const sql = "select order_info_id,order_number from applet_order_info where nickname = ? "
const sqlUpdate = "update applet_order_info set openbox_pwd = ? where nickname = ? "

var atm int64 = 0
var atm2 int64 = 0

func GetPage(args ...interface{}) C {
	in := make(C)
	go func() {
		defer close(in)
		for i := 1; i <= 100000; i++ {
			in <- i
		}
	}()
	return in

}

func GetData(in C) C {
	out := make(C)
	go func() {
		defer close(out)
		for d := range in {
			page := d.(int)
			//pagesize:=10
			//appletOrderInfoList := &AppletOrderInfoList{make([]*AppletOrderInfo, 0), page}

			appletOrderInfo := &AppletOrderInfo{}

			fmt.Sprintf("第%d条数据,\n", page)
			db := dbs.Orm.Raw(sql, "18812167612").Find(appletOrderInfo)

			if db.Error != nil {
				log.Println(db.Error)
			}
			out <- appletOrderInfo
		}
	}()
	return out
}

//模拟处理数据
func DoData(in C) C {
	out := make(C)
	go func() {
		defer close(out)
		for d := range in {
			v := d.(*AppletOrderInfo)
			//time.Sleep(time.Second*1)
			appletOrderInfo := &AppletOrderInfo{}
			atomic.AddInt64(&atm, 1)
			db := dbs.Orm.Raw(sqlUpdate, strconv.FormatInt(atomic.LoadInt64(&atm), 10), "18812167612").Find(appletOrderInfo)
			if db.Error != nil {
				log.Println(db.Error)
			}
			out <- fmt.Sprintf("%s,%s,%d,%d,%d\n", v.Id, v.OrderNumber, db.RowsAffected, time.Now().Unix(), atomic.LoadInt64(&atm))
		}
	}()
	return out
}

//func PipeTest()  {
//     p1:= NewPipe()
//     p1.SetCmd(GetPage)
//     p1.SetPipeCmd(GetData,1000)
//     out:=p1.Exec()
//
//
//     p2:= NewPipe()
//     p2.SetCmd(func(args ...interface{}) C {
//		 	return C(out)
//	 })
//	 p2.SetPipeCmd(DoData,1000)
//     out2:=p2.Exec()
//
//     for item:=range out2{
//     	fmt.Println(item)
//	 }
//
//}
